﻿// -----------------------------------------------------------------------
// <copyright file="LISPSocialLinks.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

using System.Data;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using LifeInSharePoint.Common;
using Microsoft.SharePoint;

namespace LifeInSharePoint.Blog.Common.WebControls
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class LISPSocialLinks : WebControl
    {

        private string TwitterLink = string.Empty;
        private string FacebookLink = string.Empty;
        private string LinkedInLink = string.Empty;
        private string RSSLink = string.Empty;
        private string transformedXML = null;
        private bool errorFound = false;
        private string errorMessage = null;

        private const string TwitterImage = "/_layouts/LifeInSharePoint.Blog/images/twitter_icon.png";
        private const string FacebookImage = "/_layouts/LifeInSharePoint.Blog/images/facebook_icon.png";
        private const string LinkedInImage = "/_layouts/LifeInSharePoint.Blog/images/linkedin_icon.png";
        private const string RSSImage = "/_layouts/LifeInSharePoint.Blog/images/rss_icon.png";

        private string _XSLTPath = "/_layouts/LifeInSharePoint.Blog/xslt/SocialLinks.xslt";
        public string XSLTPath
        {
            get { return _XSLTPath; }
            set { _XSLTPath = value; }
        }


        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!string.IsNullOrEmpty(XSLTPath))
            {
                TwitterLink = SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Site.RootWeb, "BlogTwitterUrl");
                FacebookLink = SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Site.RootWeb, "BlogFacebookUrl");
                LinkedInLink = SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Site.RootWeb, "BlogLinkedInUrl");
                RSSLink = SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Site.RootWeb, "BlogRSSUrl");
                processData();
            }
            else
            {
                errorFound = true;
                errorMessage = "XSLT Path is not Defined - Please edit webpart Properties";
            }

        }

        protected override void CreateChildControls()
        {
            if (!errorFound)
            {
                HtmlGenericControl container = new HtmlGenericControl("Div");
                container.InnerHtml = transformedXML;
                this.Controls.Add(container);
            }
            else
            {
                Label errorMsg = new Label();
                errorMsg.Text = errorMessage;
                this.Controls.Add(errorMsg);
            }

        }


        private void processData()
        {
            DataTable LinkDT = new DataTable("Link");
            LinkDT.Columns.Add("Image");
            LinkDT.Columns.Add("Url");

            if (!String.IsNullOrEmpty(TwitterLink))
                LinkDT.Rows.Add(TwitterImage, TwitterLink);

            if (!String.IsNullOrEmpty(FacebookLink))
                LinkDT.Rows.Add(FacebookImage, FacebookLink);

            if (!String.IsNullOrEmpty(LinkedInLink))
                LinkDT.Rows.Add(LinkedInImage, LinkedInLink);

            if (!String.IsNullOrEmpty(RSSLink))
                LinkDT.Rows.Add(RSSImage, RSSLink);


            DataSet LinksDS = new DataSet("Links");
            LinksDS.Tables.Add(LinkDT);
            string resultXML = LinksDS.GetXml();

            transformedXML = SPWebPartHelper.ConvertXML(resultXML, SPContext.Current.Site.Url + XSLTPath);
        }
    }
}
